package com.haoshuang.zhouzhoubang.ui.page.area

import com.haoshuang.zhouzhoubang.model.api.ApiRepo
import com.haoshuang.zhouzhoubang.model.bean.CityBean
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.net.ResponseBean
import com.mufeng.libs.net.netRequest
import com.mufeng.libs.state.StateData
import com.mufeng.libs.state.StatefulMutableLiveData

class ServiceAreaViewModel : BaseViewModel() {

    /**
     * 获取省
     */
    val getProvinceData = StatefulMutableLiveData<ResponseBean<MutableList<CityBean>>>()
    fun getProvince() = netRequest {
        start { getProvinceData.value = StateData.Loading }
        request { ApiRepo.getRegionNameByParentId()  }
        success { getProvinceData.value = StateData.Success(it) }
        error { code, msg ->
            getProvinceData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取市
     */
    val getCityData = StatefulMutableLiveData<ResponseBean<MutableList<CityBean>>>()
    fun getCity(parentId: String) = netRequest {
        start { getCityData.value = StateData.Loading }
        request { ApiRepo.getRegionNameByParentId(parentId)  }
        success { getCityData.value = StateData.Success(it) }
        error { code, msg ->
            getCityData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取县区
     */
    val getAreaData = StatefulMutableLiveData<ResponseBean<MutableList<CityBean>>>()
    fun getArea(parentId: String) = netRequest {
        start { getAreaData.value = StateData.Loading }
        request { ApiRepo.getRegionNameByParentId(parentId)  }
        success { getAreaData.value = StateData.Success(it) }
        error { code, msg ->
            getAreaData.value = StateData.Error(code, msg)
        }
    }
    
}